import { defineStore } from 'pinia'
import { getContractDetailApi } from '../api/signs.js'

export const useContractStore = defineStore('contract', {
	state: () => {
		return {
			// 保存当前签约详情
			contractDetail: null,
			// 保存用户信息
			userInfo: null,
			// 保存签约信息
			contractInfo: null,
			// 保存签约申请详情（用于进度跟踪）
			contractApplication: null,
			// 签约状态：0-已驳回，1-待审核，2-待支付，3-已签约，4-待续约
			contractState: 0
		}
	},
	actions: {
		// 获取并保存签约详情到状态机，原样保存不做处理
		async getContractDetailAction(contractId) {
			const res = await getContractDetailApi({
				_id: contractId
			})

			if (res.code === 200) {
				// 原样保存API返回的数据，不做任何处理
				this.contractDetail = res.data;
				this.userInfo = res.data.residentId || null;
				this.contractInfo = res.data;
				this.contractState = res.data.state !== undefined ? res.data.state : 0;

				return res.data;
			} else {
				throw new Error(res.message);
			}
		},

		// 保存签约申请详情到状态机
		saveContractApplication(applicationData) {
			this.contractApplication = {
				...applicationData,
				state: 1, // 默认状态为待审核
				submitTime: new Date().toLocaleString('zh-CN'),
				contractId: applicationData.contractId || null
			};

			// 更新签约状态
			this.contractState = 1; // 待审核状态

			console.log('签约申请详情已保存到状态机:', this.contractApplication);
		},

		// 更新签约状态
		updateContractState(newState) {
			this.contractState = newState;

			// 如果有签约申请详情，也更新其状态
			if (this.contractApplication) {
				this.contractApplication.state = newState;
			}

			// 如果有签约详情，也更新其状态
			if (this.contractDetail) {
				this.contractDetail.state = newState;
			}
		}
	},
	getters: {
		// 判断是否有签约详情
		hasContractDetail: (state) => {
			return state.contractDetail !== null
		},

		// 判断是否有签约申请详情
		hasContractApplication: (state) => {
			return state.contractApplication !== null
		},

		// 获取当前签约状态
		currentContractState: (state) => {
			if (state.contractState !== undefined) return state.contractState
			if (state.contractDetail?.state !== undefined) return state.contractDetail.state
			return 0
		},

		// 判断是否签约成功（状态为3）
		isContractSuccess: (state) => {
			let currentState = 0
			if (state.contractState !== undefined) currentState = state.contractState
			else if (state.contractDetail?.state !== undefined) currentState = state.contractDetail.state
			return currentState === 3
		},

		// 获取签约状态文本
		contractStateText: (state) => {
			let currentState = 0
			if (state.contractState !== undefined) currentState = state.contractState
			else if (state.contractDetail?.state !== undefined) currentState = state.contractDetail.state
			const stateMap = {
				0: '已驳回',
				1: '待审核',
				2: '待支付',
				3: '已签约',
				4: '待续约'
			}
			return stateMap[currentState] || '未知状态'
		},

		// 获取签约进度信息
		contractProgress: (state) => {
			let currentState = 0
			if (state.contractState !== undefined) currentState = state.contractState
			else if (state.contractDetail?.state !== undefined) currentState = state.contractDetail.state
			const stateMap = {
				0: '已驳回',
				1: '待审核',
				2: '待支付',
				3: '已签约',
				4: '待续约'
			}
			return {
				state: currentState,
				text: stateMap[currentState] || '未知状态',
				isRejected: currentState === 0,
				isWaitingReview: currentState === 1,
				isWaitingPayment: currentState === 2,
				isContractActive: currentState === 3,
				isWaitingRenewal: currentState === 4
			}
		}
	}
})
